package com.ethink.platform.manager;

import cn.hutool.core.bean.BeanUtil;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.warehouse.api.MateInventoryFacade;
import com.ethink.warehouse.api.info.MateInventoryOtherInLogDetailInfo;
import com.ethink.warehouse.api.model.MateInventoryOtherInLogDetailExcelImportModel;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 辉辉 2023/2/9 13:17
 */
@Component
public class MateInventoryOtherInLogManager {

    @Autowired
    private ExcelImportManager excelImportManager;

    @Autowired
    private MateInventoryFacade mateInventoryFacade;


    public List<MateInventoryOtherInLogDetailInfo> importMateInventoryOtherInLogDetail(MultipartFile file) {
        List<MateInventoryOtherInLogDetailExcelImportModel> excelRecordList = excelImportManager.transferClass(file, MateInventoryOtherInLogDetailExcelImportModel.class);
        // 必填项不能为空
        List<String> mateSkuList = excelRecordList.stream().map(MateInventoryOtherInLogDetailExcelImportModel::getMateSku).collect(Collectors.toList());
        Integer num = mateInventoryFacade.countByMateSkuList(mateSkuList);
        if (num < excelRecordList.size()) {
            throw new BusinessException(OrderRespCode.MATE_SKU_EXCEL_IS_NOT_EXIST);
        }
        List<MateInventoryOtherInLogDetailInfo> res = new ArrayList<>();
        excelRecordList.forEach(item -> {
            if (StringUtils.isEmpty(item.getMateSku()) || ObjectUtils.isEmpty(item.getInventoryType()) || StringUtils.isEmpty(item.getProduceCode()) || ObjectUtils.isEmpty(item.getExceptCostPrice()) || ObjectUtils.isEmpty(item.getRkTime()) || ObjectUtils.isEmpty(item.getQty())) {
                throw new BusinessException(CommonRespCode.SYSTEM_ERR, "必填项必须填");
            }
            MateInventoryOtherInLogDetailInfo mateInventoryOtherInLogDetailInfo = new MateInventoryOtherInLogDetailInfo();
            BeanUtil.copyProperties(item, mateInventoryOtherInLogDetailInfo, true);
            res.add(mateInventoryOtherInLogDetailInfo);
        });
        return res;
    }
}
